package com.yueshang.neuron.modular.system.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.yueshang.common.support.BeanKit;
import com.yueshang.neuron.modular.system.api.IUserService;
import com.yueshang.neuron.modular.system.dao.mapper.UserAssistantMapper;
import com.yueshang.neuron.modular.system.dao.mapper.UserDeptMapper;
import com.yueshang.neuron.modular.system.dao.mapper.UserManageDeptMapper;
import com.yueshang.neuron.modular.system.dao.mapper.UserMapper;
import com.yueshang.neuron.modular.system.model.SysUserAssistant;
import com.yueshang.neuron.modular.system.model.SysUserDept;
import com.yueshang.neuron.modular.system.model.SysUserManageDept;
import com.yueshang.neuron.modular.system.model.User;
import com.yueshang.neuron.modular.system.vo.req.AddUserReq;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


/**
 * 后台管理用户表(sys_user)表服务实现类
 *
 * @author shililu
 * @since 2018-08-08 13:02:48
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserDeptMapper userDeptMapper;

    @Autowired
    private UserManageDeptMapper userManageDeptMapper;

    @Autowired
    private UserAssistantMapper userAssistantMapper;


    @Override
    public List<Map<String, Object>> selectUsers(Page<User> page, String searchCondition, Long deptid, Long roleId) {
        return userMapper.selectUsers(page, searchCondition, deptid, roleId);
    }

    @Override
    @Transactional(readOnly = false)
    public void editUser(AddUserReq userReq) {
        if (userReq.getId() == null) {
            User user = new User();
            BeanKit.copyProperties(userReq, user);
            this.save(user);

            addUserRelation(userReq, user.getId());
        } else {
            this.updateById(userReq);
            delUserRelation(userReq.getId());
            addUserRelation(userReq, userReq.getId());
        }
    }

    private void delUserRelation(String userId) {
        userDeptMapper.delUserDeptByUserId(userId);
        userManageDeptMapper.delUserManageDeptByUserId(userId);
        userAssistantMapper.delUserAssistantByUserId(userId);
    }

    private void addUserRelation(AddUserReq userReq, String userId) {
        //添加部门
        for (String deptid : userReq.getDeptIds().split(",")) {
            SysUserDept sysDept = new SysUserDept();
            sysDept.setDeptId(deptid);
            sysDept.setUserId(userId);
            sysDept.insert();
        }

        //添加负责部门
        if (StringUtils.isNotBlank(userReq.getManageDeptIds())) {
            for (String deptid : userReq.getManageDeptIds().split(",")) {
                SysUserManageDept sysUserManageDept = new SysUserManageDept();
                sysUserManageDept.setDeptId(deptid);
                sysUserManageDept.setUserId(userId);
                sysUserManageDept.insert();
            }
        }

        //添加助理
        if (StringUtils.isNotBlank(userReq.getAssistantIds())) {
            for (String assistantId : userReq.getAssistantIds().split(",")) {
                SysUserAssistant sysUserAssistant = new SysUserAssistant();
                sysUserAssistant.setAssistantId(assistantId);
                sysUserAssistant.setUserId(userId);
                sysUserAssistant.insert();
            }
        }
    }
}